package pagecode.pages.cargarDatos;

import java.io.File;
import java.io.FileInputStream;


import javax.faces.component.html.HtmlCommandLink;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.cargarDatos.LogModel;

import org.apache.log4j.Logger;

import pagecode.PageCodeBase;

//import com.ibm.faces.component.html.HtmlGraphicImage;
import com.stefanini.pe.bbva.mbl.in.utils.Constantes;
//import com.ibm.faces.component.html.HtmlScriptCollector;
import javax.faces.component.html.HtmlForm;
import org.ajax4jsf.ajax.html.AjaxPoll;
import org.apache.myfaces.custom.div.Div;

import javax.faces.component.html.HtmlGraphicImage;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlPanelGroup;
import javax.faces.component.html.HtmlInputHidden;

public class LogBean extends PageCodeBase {

	private static Logger logger = Logger.getLogger(LogBean.class);
	protected LogModel logModel;
	protected HtmlGraphicImage image2;
	protected HtmlCommandLink lnkRegresar;
	protected HtmlOutputText txthplRegresar;
	public String estadoReproceso;
	protected HtmlOutputText serverDate;
//	protected HtmlScriptCollector scriptCollector1;
	protected HtmlForm form1;
	protected HtmlCommandLink lnkInicio;
	protected HtmlOutputText txthplInicio;
	protected AjaxPoll pollFecha;
	protected HtmlOutputText lblFechaPrincipal_;
	protected AjaxPoll poll;
	protected Div divPoll;
	protected HtmlOutputText lblUsuario1;
	protected HtmlOutputText lblFechaPrincipal;
	protected HtmlCommandLink lnkCerrarSession;
	protected HtmlOutputText lblCerrarSesion;
	protected HtmlOutputText lblTitulo;
//	protected HtmlScriptCollector scriptCollector2;
	protected HtmlInputText txtRutaLog;
	protected HtmlPanelGroup regionButton;
	protected HtmlPanelGroup getFechaActual;
	protected HtmlPanelGroup grid;
	protected HtmlGraphicImage imageColor;
	protected HtmlOutputText lblUsuario;
	protected HtmlInputHidden rutaContexto;
	protected HtmlOutputText txtError;
	public void onPageLoadBegin(FacesContext facescontext) {
		logger.info("Inicio Constructor");
		
		if (!this.isPostBack()) {
			try {
				this.getLogModel().setRutaLog(Constantes.CONTEXT_LOG);
				HttpServletRequest req=((HttpServletRequest)facescontext.getExternalContext().getRequest());
				this.getLogModel().setRutaContexto(req.getContextPath());
				
			} catch (Exception e) {
				this.getLogModel().setLblError_Text(
						"Ocurrio un error al ");
				logger.error(e.getMessage(), e);
			}
		}
		
		logger.info("Fin");
	}
	
	private boolean isPostBack() {
		logger.info("Inicio");
		
		if (this.getFacesContext().getExternalContext()
				.getRequestParameterMap().get("isPostLog") == null) {
			logger.info("Fin");
			
			return false;
		} else {
			logger.info("Fin");
			
			return true;
		}
	}
	/*public String doBtnDescargar(){
		try {
			File fichero = new File(this.getLogModel().getRutaLog());
			ServletContext servletContext = ((ServletContext)this.getFacesContext().getExternalContext().getContext());
			String realPath=servletContext.getRealPath("/");
			//String rutaPlantilla = realPath.concat("reportes\\").concat(UConstante.PLANTILLA_DOCUMENTO_PRODUCCION);
			logger.info("Inicio realPath:"+realPath);
			String nombreArchivo = fichero.getName();
			String rutaTemp = realPath.concat("/temp");
			String nombreArchivoTemp=crearFileTemporal(rutaTemp,nombreArchivo,fichero);
			
			logger.info("Inicio File:"+nombreArchivoTemp);
			
			
			
			logger.info("Fin");
		} catch (Exception e) {
			this.getLogModel().setLblError_Text(
					"No pudo encontrar el archivo: "+this.getLogModel().getRutaLog());
			logger.error(e.getMessage(), e);
		}
		
		return "";
	}*/
	public String doBtnDescargar(){
		try {
			logger.info("Inicio");
			
			File fichero = new File(this.getLogModel().getRutaLog());
			FacesContext ctx = FacesContext.getCurrentInstance();
			FileInputStream fis = new FileInputStream(fichero);
			byte[] bytes = new byte[1000];
			int read = 0;
			String fileName = null;
			String contentType = null;
			HttpServletResponse response =  null;
			ServletOutputStream out = null;

			//if (!ctx.getResponseComplete()) {
			   fileName = fichero.getName();
			   contentType = "text/plain";
			   
			   response = (HttpServletResponse) ctx.getExternalContext().getResponse();

			   response.setContentType(contentType);

			   response.setHeader("Content-Disposition",
			                      "attachment;filename=\"" + fileName + "\"");

			   out = response.getOutputStream();

			   while ((read = fis.read(bytes)) != -1) {
			        out.write(bytes, 0, read);
			   }

			   out.flush();
			   out.close();
			   System.out.println("\nDescargado\n");
			   //ctx.responseComplete();
			//}
			
			logger.info("Fin");
		} catch (Exception e) {
			this.getLogModel().setLblError_Text(
					"No pudo encontrar el archivo: "+this.getLogModel().getRutaLog());
			logger.error(e.getMessage(), e);
		}
		
		return "";
	}
	
	protected HtmlGraphicImage getImage2() {
		logger.info("Inicio");
		
		if (image2 == null) {
			image2 = (HtmlGraphicImage) findComponentInRoot("image2");
		}
		logger.info("Fin");
		
		return image2;
	}

	protected HtmlCommandLink getLnkRegresar() {
		logger.info("Inicio");
		
		if (lnkRegresar == null) {
			lnkRegresar = (HtmlCommandLink) findComponentInRoot("lnkRegresar");
		}
		logger.info("Fin");
		
		return lnkRegresar;
	}

	protected HtmlOutputText getTxthplRegresar() {
		logger.info("Inicio");
		
		if (txthplRegresar == null) {
			txthplRegresar = (HtmlOutputText) findComponentInRoot("txthplRegresar");
		}
		logger.info("Fin");
		
		return txthplRegresar;
	}

	protected HtmlOutputText getServerDate() {
		logger.info("Inicio");
		
		if (serverDate == null) {
			serverDate = (HtmlOutputText) findComponentInRoot("serverDate");
		}
		logger.info("Fin");
		
		return serverDate;
	}

	protected LogModel getLogModel() {
		logger.info("Inicio");
		
		if (logModel == null) {
			logModel = (LogModel) getFacesContext().getApplication()
					.createValueBinding("#{logModel}").getValue(
							getFacesContext());
		}
		logger.info("Fin");		
		
		return logModel;
	}

	protected void setLogModel(LogModel logModel) {
		this.logModel = logModel;
	}

//	protected HtmlScriptCollector getScriptCollector1() {
//		if (scriptCollector1 == null) {
//			scriptCollector1 = (HtmlScriptCollector) findComponentInRoot("scriptCollector1");
//		}
//		return scriptCollector1;
//	}

	protected HtmlForm getForm1() {
		if (form1 == null) {
			form1 = (HtmlForm) findComponentInRoot("form1");
		}
		return form1;
	}

	protected HtmlCommandLink getLnkInicio() {
		if (lnkInicio == null) {
			lnkInicio = (HtmlCommandLink) findComponentInRoot("lnkInicio");
		}
		return lnkInicio;
	}

	protected HtmlOutputText getTxthplInicio() {
		if (txthplInicio == null) {
			txthplInicio = (HtmlOutputText) findComponentInRoot("txthplInicio");
		}
		return txthplInicio;
	}

	protected AjaxPoll getPollFecha() {
		if (pollFecha == null) {
			pollFecha = (AjaxPoll) findComponentInRoot("pollFecha");
		}
		return pollFecha;
	}

	protected HtmlOutputText getLblFechaPrincipal_() {
		if (lblFechaPrincipal_ == null) {
			lblFechaPrincipal_ = (HtmlOutputText) findComponentInRoot("lblFechaPrincipal_");
		}
		return lblFechaPrincipal_;
	}

	protected AjaxPoll getPoll() {
		if (poll == null) {
			poll = (AjaxPoll) findComponentInRoot("poll");
		}
		return poll;
	}

	protected Div getDivPoll() {
		if (divPoll == null) {
			divPoll = (Div) findComponentInRoot("divPoll");
		}
		return divPoll;
	}

	protected HtmlOutputText getLblUsuario1() {
		if (lblUsuario1 == null) {
			lblUsuario1 = (HtmlOutputText) findComponentInRoot("lblUsuario1");
		}
		return lblUsuario1;
	}

	protected HtmlOutputText getLblFechaPrincipal() {
		if (lblFechaPrincipal == null) {
			lblFechaPrincipal = (HtmlOutputText) findComponentInRoot("lblFechaPrincipal");
		}
		return lblFechaPrincipal;
	}

	protected HtmlCommandLink getLnkCerrarSession() {
		if (lnkCerrarSession == null) {
			lnkCerrarSession = (HtmlCommandLink) findComponentInRoot("lnkCerrarSession");
		}
		return lnkCerrarSession;
	}

	protected HtmlOutputText getLblCerrarSesion() {
		if (lblCerrarSesion == null) {
			lblCerrarSesion = (HtmlOutputText) findComponentInRoot("lblCerrarSesion");
		}
		return lblCerrarSesion;
	}

	protected HtmlOutputText getLblTitulo() {
		if (lblTitulo == null) {
			lblTitulo = (HtmlOutputText) findComponentInRoot("lblTitulo");
		}
		return lblTitulo;
	}

//	protected HtmlScriptCollector getScriptCollector2() {
//		if (scriptCollector2 == null) {
//			scriptCollector2 = (HtmlScriptCollector) findComponentInRoot("scriptCollector2");
//		}
//		return scriptCollector2;
//	}

	protected HtmlInputText getTxtRutaLog() {
		if (txtRutaLog == null) {
			txtRutaLog = (HtmlInputText) findComponentInRoot("txtRutaLog");
		}
		return txtRutaLog;
	}

	protected HtmlPanelGroup getRegionButton() {
		if (regionButton == null) {
			regionButton = (HtmlPanelGroup) findComponentInRoot("regionButton");
		}
		return regionButton;
	}

	protected HtmlPanelGroup getGetFechaActual() {
		if (getFechaActual == null) {
			getFechaActual = (HtmlPanelGroup) findComponentInRoot("getFechaActual");
		}
		return getFechaActual;
	}

	protected HtmlPanelGroup getGrid() {
		if (grid == null) {
			grid = (HtmlPanelGroup) findComponentInRoot("grid");
		}
		return grid;
	}

	protected HtmlGraphicImage getImageColor() {
		if (imageColor == null) {
			imageColor = (HtmlGraphicImage) findComponentInRoot("imageColor");
		}
		return imageColor;
	}

	protected HtmlOutputText getLblUsuario() {
		if (lblUsuario == null) {
			lblUsuario = (HtmlOutputText) findComponentInRoot("lblUsuario");
		}
		return lblUsuario;
	}

	protected HtmlInputHidden getRutaContexto() {
		if (rutaContexto == null) {
			rutaContexto = (HtmlInputHidden) findComponentInRoot("rutaContexto");
		}
		return rutaContexto;
	}

	protected HtmlOutputText getTxtError() {
		if (txtError == null) {
			txtError = (HtmlOutputText) findComponentInRoot("txtError");
		}
		return txtError;
	}

	
	
	
	
	
	
}
